<template>
  <!-- 仓库列表 -->
  <div class="content">
    <jc-button
      :btn-arr="['reload', 'pass', 'sub', 'option', 'remove', bomCost('per:insertStock') && 'newy']"
      :disab-arr="['copy', 'close', 'cancel', 'com1', 'com2', 'chart', 'freeze']"
      @newly="newly"
      @submit="submit(1)"
      @noSubmit="submit(2)"
      @audit="audit(1)"
      @noAudit="audit(2)"
      @startUsing="using(1)"
      @forbidden="using(2)"
      @del="delet"
      @refresh="handleGetUntreated"
      @toExcel="handleExport"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQuerySonClass"
        />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="handleQuerySonClass"
        />
      </div>
    </div>
    <div class="table-content">
      <div class="left-mainPage">
        <el-tree
          node-key="fid"
          :data="materialGroupL"
          :props="defaultProps"
          :default-expanded-keys="[0]"
          check-on-click-node
          accordion
          @node-click="handleNodeClick"
        />
      </div>
      <div class="right-mainPage">
        <jc-table
          v-loading="loading"
          :table-data="tableData"
          :table-header="tableHeader"
          :table-height="tableHeight"
          serial
          sortable
          table-selection
          get-row-keys="fstockId"
          @selectionChange="handleSelectAll"
          @clickRow="detailPurchase"
        >
          <!--仓库编号-->
          <template v-slot:btnStates="col">
            <el-link type="primary" @click="detailPurchase(col.scope.row)">{{ col.scope.row.fnumber }}</el-link>
          </template>
          <!--        审核状态-->
          <template v-slot:btnState="col">
            <el-tag :type="col.scope.row.stateColor">{{ col.scope.row.fdocumentstatus }}</el-tag>
          </template>
          <!--        禁用状态-->
          <template v-slot:tagSlot="col">
            <el-tag v-if="col.scope.row.fforbidstatus === 'A'">{{ $t('market.normal') }}</el-tag>
            <el-tag v-else type="danger">{{ $t('btn.disab') }}<!--禁用--></el-tag>
          </template>
        </jc-table>
        <!--    分页器-->
        <div class="footer">
          <jc-pagination
            :total="total"
            :page.sync="currentPage"
            :limit.sync="size"
            @pagination="handleGetUntreated"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  queryPoorderLists,
  notsubmitStock, submitStock,
  notreviewStock, reviewStock,
  disableStock, ableStock, queryStockGroup, deleteStock
} from '@/api/repertory/stock'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import { toMxBmina } from '@/components/ToMxamineState'
import jcButton from '@/components/Button'

export default {
  // name: 'ForthwithRep',
  components: {
    search,
    jcButton,
    filtration
  },
  mixins: [searData],
  data() {
    return {
      materialGroupL: [{
        fname: '全部',
        fid: 0,
        details: []
      }], // tree组数据
      defaultProps: {
        children: 'details',
        label: 'fname'
      },
      ftype: 51,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      // 表头
      tableData: [], // 销售数据
      tableHeight: '73vh',
      tableHeader: [
        { label: this.$t('market.stock2'), prop: 'fname' },
        { label: this.$t('market.stockNumber'), type: 'states', prop: 'fnumber' },
        { label: this.$t('market.stockType'), prop: 'ftypeName' },
        { label: this.$t('market.stocker'), prop: 'fstockerName' },
        { label: this.$t('market.address'), prop: 'faddress' },
        { label: this.$t('market.documentstatus'), type: 'state', prop: 'fdocumentstatus' },
        { label: this.$t('market.forbidStatu'), type: 'tag', prop: 'fforbidstatus' }
      ],
      fnumberGroup: '',
      tableHeader1: []
    }
  },
  created() {
    this.getMaterialGroupL()
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 240px)' : '70vh'
  },
  methods: {
    newly() {
      this.$router.push({ path: '/newlyStock' })
    },
    // 获取树型控件数据
    async getMaterialGroupL() {
      const { data: RES } = await queryStockGroup({})
      this.materialGroupL[0].details = RES
    },
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          queryPoorderLists({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list.map(item => {
                if (item.fforbidstatus === 'A') {
                  item.fforbidstatus = '正常'
                } else {
                  item.fforbidstatus = '禁用'
                }
                return toMxBmina(item)
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '仓库列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 点击左侧树
    handleNodeClick(data) {
      this.fnumberGroup = data.fnumber
      this.handleGetUntreated()
    },
    // 获取仓库数据
    async handleGetUntreated(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        search: this.searCollData.concat(this.details),
        fnumberGroup: this.fnumberGroup
      }
      const RES = await queryPoorderLists(DATA)
      this.tableData = RES.data.list
      this.tableData.map(item => {
        return toMxBmina(item)
      })
      this.total = RES.data.total
      this.loading = false
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 详情
    detailPurchase(ev) {
      const id = ev.fstockId
      this.$router.push({ path: `/editStock/${id}` })
    },
    // 选中
    handleSelectAll(val) {
      this.val = val
      this.selectVal = []
      val.forEach(item => {
        item.fnumber && this.selectVal.push(item.fnumber)
      })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: this.selectVal }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.handleGetUntreated()
        this.selectVal = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    async submit(ev) {
      const api = ev === 1 ? submitStock : notsubmitStock
      this.toExamineFun(api)
    },
    // 审核
    async audit(ev) {
      const api = ev === 1 ? reviewStock : notreviewStock
      this.toExamineFun(api)
    },
    // 禁用启用
    async using(ev) {
      const api = ev === 1 ? ableStock : disableStock
      this.toExamineFun(api)
    },
    // 删除
    delet() {
      this.$confirm('确认要删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const api = deleteStock
        this.toExamineFun(api)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.content {
  @include listBom;
  .table-content {
    display: flex;
    flex-direction: row;
    .left-mainPage {
      width: 12%;
      height: calc(100vh - 220px);
      overflow: auto;
      display: flex;
      flex-direction: column;
      border: 1px solid #ccc;
      &::-webkit-scrollbar-track-piece {
        background: #fff;
      }

      &::-webkit-scrollbar {
        width: 6px;
      }

      &::-webkit-scrollbar-thumb {
        background: #eee;
        border-radius: 20px;
      }
    }
    .right-mainPage {
      width: 88%;
      margin-left: 10px;
    }
  }
  .header{
    position:relative;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
